package com.ww.springboot.boot.algorithm.leetcode1;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-05-24 09:35
 */
public class CD965单值二叉树 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1,new TreeNode(1),new TreeNode(2));

        System.out.println(isUnivalTree(treeNode));
    }


    public static boolean isUnivalTree(TreeNode root) {
        return getVal(root,root.val);
    }

    private static boolean getVal(TreeNode node,int val) {
        if(node == null){
            return true;
        }
        if(node.val != val){
            return false;
        }
        return getVal(node.left,node.val) && getVal(node.right,node.val);
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
